import javax.swing.ImageIcon;

import tools.Bimg;
import tools.Input;
import world.LevelManager;

import gui.Camera;
import gui.GameWindow;


public class MainClass 
{
	private static final long SLEEP_TIME = 50;
	private static final float SPEED = 10f;
	
	
	private static GameWindow win;
	private static Camera cam;
	public static void main(String[] args)
	{
		win = new gui.GameWindow();
		win.setVisible(true);
		
		cam = new Camera(0, 0, win.getWidth(), win.getHeight());
		cam.setBackground(Bimg.getBimg(new ImageIcon("C:\\Users\\Anders\\Datamatik\\Software Construction\\Eclipse\\Egne Projekter\\Game\\src\\images\\bg.png")));
		
		LevelManager.getInstance();	//<-- init LevelManager
		
		startGameLoop();
	}
	
	private static void startGameLoop()
	{
		long lastTickTime = System.currentTimeMillis();
		
		while(win.isVisible()) {
			
			float tickTime = (float) (System.currentTimeMillis() - lastTickTime);
			
			if(tickTime >= SLEEP_TIME) {
				
				float delayFactor =  tickTime / SLEEP_TIME;
				
				lastTickTime = System.currentTimeMillis();

				tick(delayFactor);				
			}
			else {
				try {
					Thread.sleep(SLEEP_TIME);
				}
				catch (InterruptedException e) {

					e.printStackTrace();
				}
			}
		}
	}
	
	private static void tick(float delayFactor)
	{
		float fps = 1000 / SLEEP_TIME * delayFactor;
		win.showFPS(fps);
		
		float realSpeed = SPEED * delayFactor;
		
		boolean moveCam = false;
		
		switch(Input.getLastAction()) {
			case LEFT:
			cam.moveX(-realSpeed);
			moveCam = true;
			break;
			case RIGHT:
			cam.moveX(realSpeed);
			moveCam = true;
			break;
			case UP:
			cam.moveY(-realSpeed);
			moveCam = true;
			break;
			case DOWN:
			cam.moveY(realSpeed);
			moveCam = true;
			break;
		}
		//if(moveCam) {
			win.renderBackground(cam);
			win.renderPlayer(cam);
			win.renderLevel(cam);
		//}
	}
}
